/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dnbW.uiW.queryW.modelW;

import dnbW.dbW.PathExpression;
import dnbW.entityW.*;
import java.util.List;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 *
 * @author zhouyong
 */
public class JoinNodeObject extends ConditionNodeLeafObject {
    // <editor-fold desc="Left Path">
    private PathExpression leftPath;

    public PathExpression getLeftPath() {
        return leftPath;
    }

    public void setLeftPath(PathExpression leftPath) {
        PathExpression oldPath = this.leftPath;
        this.leftPath = leftPath;
        propertyChangeSupport.firePropertyChange("leftPath", oldPath, this.leftPath);
    }
    // </editor-fold>

    // <editor-fold desc="Right Path">
    private PathExpression rightPath;
    
    public PathExpression getRightPath() {
        return rightPath;
    }

    public void setRightPath(PathExpression rightPath) {
        PathExpression oldPath = this.rightPath;
        this.rightPath = rightPath;
        propertyChangeSupport.firePropertyChange("rightPath", oldPath, this.rightPath);
    }
    // </editor-fold>

    // <editor-fold desc="Operator">
    private String operator;

    public String getOperator() {
        return operator;
    }
    public void setOperator(String operator) {
		this.operator = operator;
	}
    // </editor-fold>

    // <editor-fold desc="In Database">
    private boolean inDatabase = false;

    public boolean isInDatabase() {
        return inDatabase;
    }

    public void setInDatabase(boolean inDatabase) {
        this.inDatabase = inDatabase;
    }
    // </editor-fold>

    public JoinNodeObject(DefaultMutableTreeNode node,
            PathExpression leftPath,
            PathExpression rightPath,
            String operator) {
        //super(node);
        this.leftPath = leftPath;
        this.rightPath = rightPath;
        this.operator = operator;
    }

    //@Override
    public String getXPath(PathExpression outputPath) {
        if (!outputPath.isSameDataSource(this.getLeftPath()) 
                || !outputPath.isSameDataSource(this.getRightPath())) {
            return null;
        }

        int lcp1 = PathExpression.lcp(leftPath, outputPath);
        int lcp2 = PathExpression.lcp(rightPath, outputPath);

        List<DefaultMutableTreeNode> list1 = outputPath.getFullPath();
        List<DefaultMutableTreeNode> list2 = leftPath.getFullPath();
        List<DefaultMutableTreeNode> list3 = rightPath.getFullPath();

        int lcp = (lcp1 < lcp2) ? lcp1 : lcp2;

        StringBuilder result = new StringBuilder();
        for (int i = 0; i <= lcp; i++) {
            result.append("/");
            result.append(list1.get(i));
        }

        result.append("[");
        for (int i = lcp + 1; i < list2.size(); i++) {
            if (i != (lcp + 1)) {
                result.append("/");
            }
            result.append(list2.get(i));
        }
        
        if(leftPath.getFromIndex() != -1){
            result.append("[");
            result.append(leftPath.getFromIndex());
            result.append("]");
        }
        
        result.append(this.getOperator());
        
        for (int i = lcp + 1; i < list3.size(); i++) {
            if (i != (lcp + 1)) {
                result.append("/");
            }
            result.append(list3.get(i));
        }
        
        if(rightPath.getFromIndex() != -1){
            result.append("[");
            result.append(rightPath.getFromIndex());
            result.append("]");
        }        
        
        result.append("]");
        
        for(int i=lcp + 1; i<list1.size(); i++){
            result.append("/" + list1.get(i));
        }        

        return result.toString();
    }

    //@Override
    public String getXPath(PathExpression outputPath, ConditionCluster CurrentConditionCluster) {
        return null;
    }

    //@Override
    public String getXQuery(PathExpression outputPath) {
        return null;
    }

    public Condition getCondition(PathExpression outputPathExp) {
        Path outputPath = Path.buildPathFromPathExp(outputPathExp.getDataSource(), outputPathExp.getFullPathText());
        Condition outputCondition = new Condition(outputPath);

        PathExpression pathExp1, pathExp2;
        if (this.leftPath.isSameDataSource(outputPathExp)) {
            pathExp1 = this.leftPath;
            pathExp2 = this.rightPath;
        }
        else if (this.rightPath.isSameDataSource(outputPathExp)) {
            pathExp1 = this.rightPath;
            pathExp2 = this.leftPath;
        }
        else return null;

        Path path1 = Path.buildPathFromPathExp(pathExp1.getDataSource(), pathExp1.getFullPathText());
        Condition condition1 = new Condition(path1);
        outputCondition.getGroup().addCondition(condition1);

        Path path2 = Path.buildPathFromPathExp(pathExp2.getDataSource(), pathExp2.getFullPathText());
        Condition condition2 = new Condition(path2);
        condition1.getGroup().addCondition(condition2);
        
        return outputCondition;
    }
}
